home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / SHELL_SO / STDIOPRO.C < prev    next >
Text File  |  1992-12-02  |  10KB  |  393 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  *
  55.  *
  56.  *    Psuedo process function file
  57.  *
  58.  *    Ñ╩Standard Input Function
  59.  *
  60.  *    Ñ╩Standard Output Function
  61.  *
  62.  *    Ñ╩Standard Error Output Function
  63.  */
  64. #include    <FileMgr.h>
  65. #include    "System.h"
  66. #include    "Struct.h"
  67. #include    "proc.h"
  68. #include    "Shell.h"
  69. #include    "path.h"
  70. #include    "EditU.h"
  71.  
  72. #define        BUFSIZE        256
  73.  
  74. /*******************************************************************/
  75.  
  76. Boolean        StdInProc( int ProcToken, WHandle ShellWh, int ProcID,
  77.                 char *string )
  78. {
  79. int            pwdVRefNum = 0, err;
  80. pathType    pt;
  81. char        readBuf[ BUFSIZE ], *cp;
  82. long        parDirID = 0L, pwdDirID = 0L, count = BUFSIZE - 1;
  83. ShellWindRec    **MyShell;
  84.  
  85.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  86.  
  87.     switch( ProcToken )
  88.         {
  89.         case    PROC_INIT    :
  90.             /* if a string, then open it as a file */
  91.  
  92.             (**MyShell).Proc[ ProcID ].fileRefNum =
  93.                  OpenFileDirect( string, 'TEXT', fsRdPerm );
  94.             ResetShellPWD( ShellWh );
  95.  
  96.             if( (**MyShell).Proc[ ProcID ].fileRefNum )
  97.                 {
  98. #ifdef    PROCDEBUG
  99.                 printf( "Openned STDIN file %ps with err %d\n",
  100.                     string, err );
  101. #endif
  102.                 return( TRUE );
  103.                 }
  104.             return( FALSE );
  105.             
  106.          case    PROC_TERM    :
  107.         case    PROC_BREAK    :
  108.             /* if a file open, close it */
  109.             if( (**MyShell).Proc[ ProcID ].fileRefNum )
  110.                 {
  111.                 FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
  112.                 (**MyShell).Proc[ ProcID ].fileRefNum = 0;
  113.                 }
  114.                 
  115.             SendOutToken( ShellWh, ProcID, PROC_BREAK  );
  116.  
  117.             /* Turn ourself off */
  118.             if( ProcID != STDIN_PROCID )
  119.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  120.             else
  121.                 (**MyShell).Proc[ ProcID ].StdOutProcID = CL_PROCID;
  122.  
  123. #ifdef    PROCDEBUG
  124.             printf( "Reset STDIN Proc to Shell\n" );
  125. #endif            
  126.             break;
  127.             
  128.         case    PROC_STDIN    :
  129.             /* if there is a file open, read a line and send it, return TRUE */
  130.             if( (**MyShell).Proc[ ProcID ].fileRefNum )
  131.                 {
  132.                 err = FSRead( (**MyShell).Proc[ ProcID ].fileRefNum, &count, readBuf );
  133.             
  134.                 readBuf[ count ] = '\0';
  135.                 
  136.                 if( err == eofErr )    /* if we are at the end of file */
  137.                     {
  138.                     StdOut( ShellWh, ProcID, readBuf );
  139.                     
  140.                     /* send a break to StdOut */
  141.                     SendOutToken( ShellWh, ProcID, PROC_BREAK );
  142.  
  143.                     /* if a file open, close it */
  144.                     FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
  145.                     (**MyShell).Proc[ ProcID ].fileRefNum = 0;
  146.  
  147.                     /* Turn ourself off */
  148.                     if( ProcID != STDIN_PROCID )
  149.                         (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  150.                     }
  151.                 else if( err )
  152.                     {
  153.                     /* send a break to StdOut */
  154.                     SendOutToken( ShellWh, ProcID, PROC_BREAK );
  155.  
  156.                     /* if a file open, close it */
  157.                     FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
  158.                     (**MyShell).Proc[ ProcID ].fileRefNum = 0;
  159.  
  160.                     /* Turn ourself off */
  161.                     if( ProcID != STDIN_PROCID )
  162.                         (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  163.                     }
  164.                 else
  165.                     StdOut( ShellWh, ProcID, readBuf );
  166.                 }
  167.                 
  168.             else if( string )    /* write it out to the stream */
  169.                 StdOut( ShellWh, ProcID, string  );
  170.                 
  171.             else if( (**ShellWh).entryCnt == 0 )
  172.                 /* Look in the input PE for a character string */
  173.                 {
  174.                 char        command[ CMDLINESIZE ], *sp, *cp, **sh, *base;
  175.                 int32        peLen, cmdLen = 0L, prompt;
  176.                 PEHandle    hPE;
  177.                 Boolean        found = FALSE;
  178.                 
  179.                 hPE = (**MyShell).ihPE;
  180.                 sh = (**hPE).hText;
  181.                 HLock( sh );
  182.                 
  183.                 sp = *sh;
  184.                 cp = command;
  185.     
  186.                 peLen = (**MyShell).promptBase;    /* move past prompt */
  187.                 while( peLen )
  188.                     {
  189.                     sp++;
  190.                     peLen--;
  191.                     }
  192.                 base = sp;
  193.                 
  194.                 peLen = (**hPE).peLength - (**MyShell).promptBase;                    
  195.                 
  196.                 if( (*sp == '\r') && (peLen > 0))    /* NULL command */
  197.                     {
  198.                     found = TRUE;
  199.                     cmdLen++;
  200.                     }
  201.                 else
  202.                     {
  203.                     PERemoveGap( hPE );
  204.  
  205.                     while( (peLen > 0) && (cmdLen < (int32)CMDLINESIZE) )
  206.                         {
  207.                         if( *sp == '\r') 
  208.                             {
  209.                             cmdLen++;
  210.                             found = TRUE;
  211.                             break;
  212.                             }
  213.                         *cp++ = *sp++;
  214.                         cmdLen++;
  215.                         peLen--;
  216.                         }
  217.                     }
  218.                     
  219.                 if( found )        /* something to do */
  220.                     {
  221.                     register WHandler    **wh, **wh2;
  222.                     register WindowPtr    w;
  223.                     int32                saveBase = (**MyShell).promptBase;
  224.                     
  225.                     *cp++ = '\n';
  226.                     *cp = '\0';    /* complete command */
  227.  
  228.                     /* Preset undo string to the command */
  229.                     PESetSelect( (**MyShell).promptBase,
  230.                         (**MyShell).promptBase +  cmdLen-1L, hPE );
  231.             
  232.                     PresetUndo( uCLEAR, hPE );
  233.                     
  234.                     PESetSelect( 0L, (**MyShell).promptBase +  cmdLen, hPE );
  235.                     PEDelete( hPE );    /* delete the prompt and the command */
  236.                     (**MyShell).promptBase = 0L;
  237.                     
  238.                     StdOut( ShellWh, ProcID, command  );    /* do the string */
  239.                     (**MyShell).promptBase = saveBase;
  240.                     
  241.                     DoShellPrompt( ShellWh );                /* indicate command ready */
  242.                     PESetSelect( (**MyShell).promptBase,
  243.                         (**MyShell).promptBase, hPE );
  244.                         
  245.                     SetPeditEditStatus( hPE, ShellWh );
  246.                     SetUndo( hPE );
  247.                     
  248.                     prompt = (**MyShell).prompt;
  249.                     
  250.                     if( prompt != (**MyShell).prompt )
  251.                         {
  252.                         PESetSelect( 0L, (**MyShell).promptBase, hPE );
  253.                         PEDelete( hPE );
  254.                         DoShellPrompt( ShellWh );
  255.                         }
  256.                         
  257.                     AdjustInput( ShellWh, TRUE );
  258.                     PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
  259.                     }
  260.                 
  261.                 HUnlock( sh );
  262.                 }
  263.             break;
  264.             
  265.         default :
  266.             break;
  267.         }
  268.         
  269.     return( TRUE );
  270. }
  271.  
  272. /*******************************************************************/
  273.  
  274. Boolean        StdOutProc( int ProcToken, WHandle ShellWh, int ProcID,
  275.                 char *string )
  276. {
  277. int            vRefNum = 0, err;
  278. long        count;
  279. ShellWindRec    **MyShell;
  280.  
  281.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  282.  
  283.     switch( ProcToken )
  284.         {
  285.         case    PROC_INIT    :
  286.             /* if a string, then open it as a file */
  287.             
  288.             if( (**MyShell).StdOutAppend == FALSE )
  289.                 {
  290.                 if( err = CreateFile( string, 'TEXT' ) )
  291.                     return( FALSE );
  292.  
  293.                 ResetShellPWD( ShellWh );
  294.                 }
  295.  
  296.             (**MyShell).Proc[ ProcID ].fileRefNum =
  297.                 OpenFileDirect( string, 'TEXT', fsWrPerm );
  298.             ResetShellPWD( ShellWh );
  299.  
  300.             if( (**MyShell).Proc[ ProcID ].fileRefNum )
  301.                 {
  302. #ifdef    PROCDEBUG
  303.                 printf( "Openned STDOUT file %ps with err %d\n",
  304.                         string, err );
  305. #endif
  306.                 if( (**MyShell).StdOutAppend )
  307.                     SetFPos( (**MyShell).Proc[ ProcID ].fileRefNum, fsFromLEOF, 0L );
  308.                     
  309.                 return( TRUE );
  310.                 }
  311.             else    /* file did not exist create it and continue */
  312.                 {
  313.                 if( err = CreateFile( string, 'TEXT' ) )
  314.                     {
  315.                     FileError( err );
  316.                     return( FALSE );
  317.                     }
  318.  
  319.                 (**MyShell).Proc[ ProcID ].fileRefNum =
  320.                     OpenFileDirect( string, 'TEXT', fsWrPerm );
  321.                     
  322.                 ResetShellPWD( ShellWh );
  323.     
  324.                 if( (**MyShell).Proc[ ProcID ].fileRefNum )
  325.                     return( TRUE );
  326.                 }
  327.             return( FALSE );
  328.             
  329.          case    PROC_TERM    :
  330.         case    PROC_BREAK    :
  331.             /* if a file open, close it */
  332.             if( (**MyShell).Proc[ ProcID ].fileRefNum )
  333.                 {
  334.                 FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
  335.                 (**MyShell).Proc[ ProcID ].fileRefNum = 0;
  336.                 }
  337.             
  338.             /* Tell the shell that we're done */
  339.             ShellOutProc( PROC_BREAK, ShellWh, ProcID, NULL );
  340.  
  341.             /* Turn ourself off */
  342.             if( ProcID != STDOUT_PROCID )
  343.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  344.             break;
  345.             
  346.         case    PROC_STDIN    :
  347.             /* is there a file open, write the string, return TRUE */
  348.             if( (**MyShell).Proc[ ProcID ].fileRefNum && string && *string )
  349.                 {
  350.                 count = (long) strlen( string );
  351.                 err = FSWrite( (**MyShell).Proc[ ProcID ].fileRefNum,
  352.                             &count, string );
  353.                 
  354.                 if( err )
  355.                     {
  356.                     /* send a break to StdOut */
  357.                     SendOutToken( ShellWh, ProcID, PROC_BREAK );
  358.  
  359.                     /* if a file open, close it */
  360.                     FSClose( (**MyShell).Proc[ ProcID ].fileRefNum );
  361.                     (**MyShell).Proc[ ProcID ].fileRefNum = 0;
  362.  
  363.                     /* Turn ourself off */
  364.                     if( ProcID != STDOUT_PROCID )
  365.                         (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  366.                     }
  367.                 }
  368.             else if( string )    /* write it out to the shell */
  369.                 ShellOutProc( PROC_STDIN, ShellWh, ProcID, string );
  370.             
  371.             break;
  372.         }
  373. }
  374.  
  375. /*******************************************************************/
  376.  
  377. Boolean        StdErrProc( int ProcToken, WHandle ShellWh, int ProcID, char *string )
  378. {
  379.     switch( ProcToken )
  380.         {
  381.         case    PROC_INIT    :    /* redirect err output to a file */
  382.             break;
  383.             
  384.          case    PROC_TERM    :
  385.         case    PROC_BREAK    :
  386.             break;
  387.             
  388.         case    PROC_STDIN    :
  389.             ShellOutProc( PROC_STDIN, ShellWh, ProcID, string );
  390.             break;
  391.         }
  392. }
  393.